﻿package Classes.Score{

	import com.hurlant.crypto.Crypto;
	import com.hurlant.crypto.symmetric.NullPad;
	import com.hurlant.crypto.symmetric.PKCS5;
	import com.hurlant.crypto.symmetric.ICipher;
	import com.hurlant.crypto.symmetric.IPad;
	import com.hurlant.util.Base64;
	import com.hurlant.util.Hex;
	import flash.utils.ByteArray;

	public class Encrypter {

		private var type:String='simple-aes-cbc';
		private var key:ByteArray;

		public function Encrypter() {
			init();
		}

		private function init():void {
			
			key=Hex.toArray(Hex.fromString('8b515416e6372872ac18d80791b70b81'));
			
		}

		public function encrypt(txt:String = ''):String {
			var data:ByteArray=Hex.toArray(Hex.fromString(txt));
			var pad:IPad=new PKCS5  ;
			var mode:ICipher=Crypto.getCipher(type,key,pad);
			pad.setBlockSize(mode.getBlockSize());
			mode.encrypt(data);
			return Base64.encodeByteArray(data);
		}
		
		public function decrypt(txt:String = ''):String {
			var data:ByteArray=Base64.decodeToByteArray(txt);
			var pad:IPad=new PKCS5  ;
			var mode:ICipher=Crypto.getCipher(type,key,pad);
			pad.setBlockSize(mode.getBlockSize());
			mode.decrypt(data);
			return Hex.toString(Hex.fromArray(data));
		}

	}

}